package com.bl.student.service.biz.address.impl;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.utils.ListUtils;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.AddressEntity;
import com.bl.student.service.biz.address.AddressService;
import com.bl.student.service.vo.address.AddressAddVo;
import com.bl.student.service.vo.address.AddressModVo;
import com.bl.student.service.vo.address.AddressVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.bl.student.dao.repository.AddressRepository;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.DelListBody;
import java.util.Date;
import java.util.List;


/**
 * Created by liangweikang on 18/1/29.
 */
@Log4j2
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressRepository addressRepository;

    /**
     * 甲乙通用
     * @param addressAddVo
     * @param partybId
     */
    @Override
    public void add(AddressAddVo addressAddVo,String partybId) {
        AddressEntity addressEntity =  new AddressEntity();
        addressEntity.setId(UUIDUtils.getUUID());
        addressEntity.setCreateTime(new Date());
        addressEntity.setUpdateTime(new Date());
        addressEntity.setPartybId(partybId);
        addressEntity.setPartybName(addressAddVo.getName());
        addressEntity.setPartybMobile(addressAddVo.getMobile());
        addressEntity.setPartybAddress(addressAddVo.getAddress());
        addressRepository.save(addressEntity);
    }

    /**
     * 甲乙通用
     * @param addressAddVo
     * @param partybId
     */
    @Override
    public void modify(AddressModVo addressModVo) {
        AddressEntity addressEntity = addressRepository.findOne(addressModVo.getId());
//        AddressEntity addressEntity =  new AddressEntity();
        if (addressEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        addressEntity.setPartybName(addressModVo.getName());
        addressEntity.setPartybMobile(addressModVo.getMobile());
        addressEntity.setPartybAddress(addressModVo.getAddress());
        addressEntity.setUpdateTime(new Date());
        addressRepository.save(addressEntity);
    }

    /**
     * 甲乙通用
     * @param partybId
     */
    @Override
    public AddressVo getAddressVo(String partybId) {

        List<AddressEntity> addressEntities;
        AddressVo addressVo = new AddressVo();

        addressEntities = addressRepository.findByPartybIdOrderByCreateTimeDesc(partybId);
        AddressEntity addressEntity = ListUtils.getFirst(addressEntities);

        if (addressEntity != null) {
            BeanUtils.copyProperties(addressEntity, addressVo);
        }

        return addressVo;
    }

    @Override
    public void del(DelBody id) {
        addressRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for (String id : idList.getList()) {

            addressRepository.delete(id);

        }
    }
}


